import e from "./system";
import t from "./view_container";
import n from "./vec2";
import r from "./scroll_bar_style";
import i from "./scroll_bar";
import s from "./event_center";
import o from "./touch_event";

const u = {
  scrollBar(e, t) {
    return null;
  },
  onScrollViewStartScroll(e) {},
  onScrollViewScrolling(e) {},
  onScrollViewEndScroll(e) {},
  onScrollViewStartEasing(e) {},
};

export default class ScrollView extends t {
  constructor() {
    super();
    s.addListener(this, this, o.TOUCH_START, this.onViewScrollBegin);
  }
  scrolling = !1;
  delegate = null;
  dealloc() {
    let e = this;
    e._contentView != null &&
      (e._contentView.dealloc(), (e._contentView = null)),
      e._hScrollBar != null &&
        (e._hScrollBar.remove(),
        e._hScrollBar.dealloc(),
        (e._hScrollBar = null)),
      e._vScrollBar != null &&
        (e._vScrollBar.remove(),
        e._vScrollBar.dealloc(),
        (e._vScrollBar = null)),
      t.prototype.dealloc.call(e);
  }
  clone() {
    let e = this,
      t = new ScrollView();
    return e.copyProperties(t), t;
  }
  copyProperties(e) {
    let n = this;
    t.prototype.copyProperties.call(n, e),
      e.empty(),
      n._contentView != null &&
        ((e._contentView = n._contentView.clone()),
        e.addSubView(n._contentView)),
      n._hScrollBar != null &&
        ((e._hScrollBar = n._hScrollBar.clone()),
        (e._hScrollBar.scrollView = e),
        e.addSubView(n._hScrollBar)),
      n._vScrollBar != null &&
        ((e._vScrollBar = n._vScrollBar.clone()),
        (e._vScrollBar.scrollView = e),
        e.addSubView(n._vScrollBar)),
      e.offset(n.offset().clone()),
      e.contentSize(n.contentSize().clone()),
      e.paging(n._paging),
      e.bouncing(n._bouncing),
      e.lockHorizontal(n._lockHorizontal),
      e.lockVertical(n._lockVertical),
      e.scrollBarStyle(n._scrollBarStyle),
      e.easing(n._easing),
      e.reloadData();
  }
  updatePropsFromXML(e) {
    let i = this;
    t.prototype.updatePropsFromXML.call(i, e), i.clip(!0);
    let s = e.find("> content");
    s.length > 0 && i.contentView().updatePropsFromXML(s);
    if (e.is("[offset]")) {
      let o = e.attr("offset").split(","),
        u = new n(parseFloat(o[0]), parseFloat(o[1]));
      i.offset(u);
    }
    if (e.is("[content-size]")) {
      let a = e.attr("content-size").split(","),
        f = new n(parseFloat(a[0]), parseFloat(a[1]));
      i.contentSize(f);
    }
    e.is("[bouncing]") && i.bouncing(e.attr("bouncing") == "true"),
      e.is("[paging]") && i.paging(e.attr("paging") == "true"),
      e.is("[lock-h]") && i.lockHorizontal(e.attr("lock-h") == "true"),
      e.is("[lock-v]") && i.lockVertical(e.attr("lock-v") == "true"),
      e.is("[scroll-bar-style]") &&
        i.scrollBarStyle(r[e.attr("scroll-bar-style").toUpperCase()]),
      i.reloadData();
  }
  lastOffset = null;
  currentSpeed = new n();
  easingInitSpeed = null;
  easingDuration = null;
  easingAcc = null;
  desOffset = null;
  beginEasing = !1;
  easingStartTs = 0;
  easingStartOffset = null;
  update(e, r) {
    let i = this;
    t.prototype.update.call(i, e, r);
    if (i.lastOffset != null) {
      let s = i.offset().clone();
      if (i.paging() == 0 || i.lastOffset.equalsTo(s) == 0)
        (i.currentSpeed = new n()),
          (i.currentSpeed.x = (s.x - i.lastOffset.x) / r),
          (i.currentSpeed.y = (s.y - i.lastOffset.y) / r);
    }
    (i.lastOffset = i.offset().clone()),
      i.beginEasing &&
        ((i.easingStartTs = e),
        (i.easingStartOffset = i.offset().clone()),
        (i.beginEasing = !1));
    if (i.easingInitSpeed != null) {
      let o = e - i.easingStartTs,
        u = i.offset().clone();
      o < i.easingDuration.x
        ? (u.x =
            i.easingStartOffset.x +
            i.easingInitSpeed.x * o +
            (i.easingAcc.x * o * o) / 2)
        : (u.x = i.desOffset.x),
        o < i.easingDuration.y
          ? (u.y =
              i.easingStartOffset.y +
              i.easingInitSpeed.y * o +
              (i.easingAcc.y * o * o) / 2)
          : (u.y = i.desOffset.y),
        i.offset(u),
        o >= i.easingDuration.x && o >= i.easingDuration.y
          ? i.onViewEndScroll()
          : i.delegate != null && i.delegate.onScrollViewScrolling(i);
    }
    i.needReload && ((i.needReload = !1), i.renderView());
  }
  width(e?) {
    let n = this;
    return (
      isNaN(e) == 0 &&
        e != n.width() &&
        (t.prototype.width.call(n, e), n.reloadData()),
      t.prototype.width.call(n)
    );
  }
  height(e?) {
    let n = this;
    return (
      isNaN(e) == 0 &&
        e != n.height() &&
        (t.prototype.height.call(n, e), n.reloadData()),
      t.prototype.height.call(n)
    );
  }
  _contentView = null;
  contentView() {
    let e = this;
    return (
      e._contentView == null &&
        ((e._contentView = new t()),
        e._contentView.clip(!0),
        e.insertSubViewAt(e._contentView, 0)),
      e._contentView
    );
  }
  _offset = new n();
  offset(e?) {
    let t = this;
    return (
      Boolean(e) &&
        e.equalsTo(t._offset) == 0 &&
        ((t._offset = e.clone()), t.reloadData()),
      t._offset
    );
  }
  _contentSize = new n();
  contentSize(e?) {
    let t = this;
    return (
      Boolean(e) &&
        e.equalsTo(t._contentSize) == 0 &&
        ((t._contentSize = e.clone()), t.reloadData()),
      t._contentSize
    );
  }
  _bouncing = !0;
  bouncing(e?) {
    let t = this;
    return (
      typeof e == "boolean" && t._bouncing != e && (t._bouncing = e),
      t._bouncing
    );
  }
  _easing = !0;
  easing(e?) {
    let t = this;
    return (
      typeof e == "boolean" && e == t._easing && (t._easing = e), t._easing
    );
  }
  _paging = !1;
  paging(e?) {
    let t = this;
    return (
      typeof e == "boolean" && t._paging != e && (t._paging = e), t._paging
    );
  }
  _hScrollBar = null;
  hScrollBar() {
    let e = this;
    return (
      e._hScrollBar == null &&
        (e.delegate != null &&
          e.delegate.scrollBar != null &&
          (e._hScrollBar = e.delegate.scrollBar(e, !1)),
        e._hScrollBar == null && (e._hScrollBar = new i(!1)),
        (e._hScrollBar.scrollView = e),
        e.addSubView(e._hScrollBar)),
      e._hScrollBar
    );
  }
  _vScrollBar = null;
  vScrollBar() {
    let e = this;
    return (
      e._vScrollBar == null &&
        (e.delegate != null &&
          e.delegate.scrollBar != null &&
          (e._vScrollBar = e.delegate.scrollBar(e, !0)),
        e._vScrollBar == null && (e._vScrollBar = new i(!0)),
        (e._vScrollBar.scrollView = e),
        e.addSubView(e._vScrollBar)),
      e._vScrollBar
    );
  }
  _lockHorizontal = !1;
  lockHorizontal(e?) {
    let t = this;
    return (
      typeof e == "boolean" &&
        t._lockHorizontal != e &&
        (t._lockHorizontal = e),
      t._lockHorizontal
    );
  }
  _lockVertical = !1;
  lockVertical(e?) {
    let t = this;
    return (
      typeof e == "boolean" && t._lockVertical != e && (t._lockVertical = e),
      t._lockVertical
    );
  }
  _scrollBarStyle = r.AUTO;
  scrollBarStyle(e?) {
    let t = this;
    return (
      isNaN(e) == 0 &&
        e != t._scrollBarStyle &&
        ((t._scrollBarStyle = e),
        t._scrollBarStyle == r.AUTO
          ? (t._hScrollBar != null &&
              (t._hScrollBar.stopAnimation(),
              t.scrolling &&
              t.lockHorizontal() == 0 &&
              t.contentSize().x > t.width()
                ? t._hScrollBar.show()
                : t._hScrollBar.hide()),
            t._vScrollBar != null &&
              t.lockVertical() == 0 &&
              t.contentSize().y > t.height() &&
              (t._vScrollBar.stopAnimation(),
              t.scrolling ? t._vScrollBar.show() : t._vScrollBar.hide()))
          : t._scrollBarStyle == r.HIDDEN
          ? (t._hScrollBar != null &&
              (t._hScrollBar.remove(),
              t._hScrollBar.dealloc(),
              (t._hScrollBar = null)),
            t._vScrollBar != null &&
              (t._vScrollBar.remove(),
              t._vScrollBar.dealloc(),
              (t._vScrollBar = null)))
          : t._scrollBarStyle == r.VISIBLE &&
            (t.lockHorizontal() == 0 && t.hScrollBar().show(),
            t.lockVertical() == 0 && t.vScrollBar().show())),
      t._scrollBarStyle
    );
  }
  needReload = !1;
  reloadData() {
    let e = this;
    e.needReload = !0;
  }
  renderView() {
    let e = this;
    if (e.bouncing() == 0) {
      let t = e.offset();
      (t.x = Math.max(0, Math.min(e.contentSize().x - e.width(), t.x))),
        (t.y = Math.max(0, Math.min(e.contentSize().y - e.height(), t.y)));
    }
    e._contentView != null &&
      (e._contentView.width(e.contentSize().x),
      e._contentView.height(e.contentSize().y),
      e._contentView.x(-1 * e.offset().x),
      e._contentView.y(-1 * e.offset().y)),
      e._hScrollBar != null &&
        (e._hScrollBar.updateSize(), e._hScrollBar.updatePotion()),
      e._vScrollBar != null &&
        (e._vScrollBar.updateSize(), e._vScrollBar.updatePotion());
  }
  lastTouch = null;
  onViewScrollBegin(e) {
    let t = this;
    if (t.contentSize().x > t.width() || t.contentSize().y > t.height())
      (t.easingInitSpeed = null),
        (t.easingDuration = 0),
        (t.easingAcc = null),
        (t.desOffset = null),
        (t.lastTouch = new n(e.touches[0].x, e.touches[0].y)),
        s.addListener(
          t,
          t,
          [o.TOUCH_MOVE, o.TOUCH_END],
          [t.onViewScrolling, t.onViewScrollEnd]
        ),
        (t.scrolling = !0),
        t.delegate != null && t.delegate.onScrollViewStartScroll(t);
  }
  onViewScrolling(e) {
    let t = this,
      i = new n(e.touches[0].x, e.touches[0].y),
      s = t.offset().clone(),
      o = new n();
    t.lockHorizontal()
      ? (o.x = s.x)
      : s.x >= 0 && s.x <= t.contentSize().x - t.width()
      ? (o.x = s.x - (i.x - t.lastTouch.x))
      : (o.x = s.x - (i.x - t.lastTouch.x) / 4),
      t.lockVertical()
        ? (o.y = s.y)
        : s.y >= 0 && s.y <= t.contentSize().y - t.height()
        ? (o.y = s.y - (i.y - t.lastTouch.y))
        : (o.y = s.y - (i.y - t.lastTouch.y) / 4),
      t.offset(o),
      (t.lastTouch = i),
      t.delegate != null && t.delegate.onScrollViewScrolling(t),
      t.scrollBarStyle() == r.AUTO &&
        (t.lockHorizontal() == 0 &&
          t.contentSize().x > t.width() &&
          t.hScrollBar().show(),
        t.lockVertical() == 0 &&
          t.contentSize().y > t.height() &&
          t.vScrollBar().show());
  }
  BOUNCING_DURATION = 0.2;
  EASING_DURATION = 0.4;
  onViewScrollEnd() {
    let e = this,
      t = e.offset().clone(),
      r = t.clone(),
      i = 0,
      u = 0,
      a = 0,
      f = 0,
      l = e.currentSpeed.clone();
    t.x < 0
      ? ((r.x = 0),
        (i = e.BOUNCING_DURATION),
        (l.x = ((r.x - t.x) * 2) / i),
        (a = (l.x / i) * -1))
      : t.x > e.contentSize().x - e.width()
      ? ((r.x = e.contentSize().x - e.width()),
        (i = e.BOUNCING_DURATION),
        (l.x = ((r.x - t.x) * 2) / i),
        (a = (l.x / i) * -1))
      : e.paging()
      ? ((r.x = t.x + (l.x >= 0 ? 1 : -1) * e.width()),
        (r.x = Math.max(0, Math.min(r.x, e.contentSize().x - e.width()))),
        (i = e.EASING_DURATION),
        (l.x = ((r.x - t.x) * 2) / i),
        (a = (l.x / i) * -1))
      : e.easing() &&
        l.x != 0 &&
        ((i = e.EASING_DURATION),
        (r.x = t.x + (l.x * i) / 2),
        (r.x = Math.max(0, Math.min(r.x, e.contentSize().x - e.width()))),
        (l.x = ((r.x - t.x) * 2) / i),
        (a = (l.x / i) * -1)),
      t.y < 0
        ? ((r.y = 0),
          (u = e.BOUNCING_DURATION),
          (l.y = ((r.y - t.y) * 2) / u),
          (f = (l.y / u) * -1))
        : t.y > e.contentSize().y - e.height()
        ? ((r.y = e.contentSize().y - e.height()),
          (u = e.BOUNCING_DURATION),
          (l.y = ((r.y - t.y) * 2) / u),
          (f = (l.y / u) * -1))
        : e.paging()
        ? ((r.y = t.y + (l.y >= 0 ? 1 : -1) * e.height()),
          (r.y = Math.max(0, Math.min(r.y, e.contentSize().y - e.height()))),
          (u = e.EASING_DURATION),
          (l.y = ((r.y - t.y) * 2) / u),
          (f = (l.y / u) * -1))
        : e.easing() &&
          l.y != 0 &&
          ((u = e.EASING_DURATION),
          (r.y = t.y + (l.y * u) / 2),
          (r.y = Math.max(0, Math.min(r.y, e.contentSize().y - e.height()))),
          (l.y = ((r.y - t.y) * 2) / u),
          (f = (l.y / u) * -1)),
      r.equalsTo(t)
        ? e.onViewEndScroll()
        : ((e.desOffset = r),
          (e.easingInitSpeed = l),
          (e.easingDuration = new n(i, u)),
          (e.easingAcc = new n(a, f)),
          (e.beginEasing = !0),
          e.delegate != null && e.delegate.onScrollViewStartEasing(e)),
      (e.lastTouch = null),
      s.removeListener(
        e,
        e,
        [o.TOUCH_MOVE, o.TOUCH_END],
        [e.onViewScrolling, e.onViewScrollEnd]
      );
  }
  onViewEndScroll() {
    let e = this;
    (e.scrolling = !1),
      (e.easingInitSpeed = null),
      (e.easingDuration = 0),
      (e.easingAcc = null),
      (e.desOffset = null),
      e._hScrollBar != null && e._hScrollBar.hide(),
      e._vScrollBar != null && e._vScrollBar.hide(),
      e.delegate != null && e.delegate.onScrollViewEndScroll(e),
      e.scrollBarStyle() == r.AUTO &&
        (e.lockHorizontal() == 0 &&
          e.contentSize().x > e.width() &&
          e.hScrollBar().hide(),
        e.lockVertical() == 0 &&
          e.contentSize().y > e.height() &&
          e.vScrollBar().hide());
  }
}
